﻿var pHwpCtrl = null;
var bEditMode = ("READ"=="READ") ? false : true;         // 문서가 편집(Edit)상태인가를 나타내는 boolean
var NetHwpMode = "SIMPLE";                                          // FULL:전체메뉴, SIMPLE:기본메뉴
var SYS_PREFIX = "__$$ibs";
var SYS_HWPEXT = ".hwp";
var localPath = "";
details=navigator.userAgent;
if (details.indexOf("9x")         !=-1 ||
	details.indexOf("Windows ME") !=-1 ||
	details.indexOf("Win16")      !=-1 ||
	details.indexOf("95")         !=-1 ||
	details.indexOf("98")         !=-1 ||
	details.indexOf("NT 5.1")     !=-1 ||
	details.indexOf("NT 6.0")
   )
	localPath = "C:\\WINDOWS\\Temp\\";
else
if (details.indexOf("NT 5.0")     !=-1)
	localPath = "C:\\WINNT\\Temp\\";
if(localPath == "") alert("시스템환경이 일치하지 않습니다. 관리자에게 문의 바랍니다.");
var aURL = "http://" + location.host + "/iFrame/DOWN/";
var sURL = aURL + "0";
var bNewWindow = ("0"=="0") ? false : true;                   // 새창으로 첨부HWP파일 보기
// 문서가 새로운 문서인가를 나타내는 boolean
var bNewDoc = true;
var InitInfo = "5000*10"
var nMaxSize  = InitInfo.split("*")[0];
var nMaxCount = InitInfo.split("*")[1];

function initNetHwp() {
	pHwpCtrl = document.all.HwpCtrl;

	// 넷한글에서만 사용되어지는 초기화 부분입니다.
	pHwpCtrl.SetClientName("RELEASE");
	pHwpCtrl.SetClientName("SITENAME:nethangul_si");
	pHwpCtrl.SetClientName("USER:industrial_bank");
	pHwpCtrl.SetClientName("NETHWP");
	pHwpCtrl.Run("LogoOff");
	if (bEditMode) editToolBar();
	else readToolBar();
	pHwpCtrl.ShowToolBar(true);
	pHwpCtrl.ShowStatusBar(false);
	pHwpCtrl.AutoShowHideToolBar(true);

	initViewProp();
}
function initViewProp() {
	var vp = pHwpCtrl.ViewProperties;
//	var vp = pHwpCtrl.CreateSet("ViewProperties");
	vp.SetItem("OptionFlag", 1);  // 쪽윤곽 - Off
	vp.SetItem("ZoomType", 0);    // 화면확대종류 - 0=사용자정의, 1=쪽맞춤, 2=폭맞춤, 3=여러쪽
	vp.SetItem("ZoomRatio", 100); // 화면확대비율 - 100%
	pHwpCtrl.ViewProperties = vp;

	// 페이지 가로크기 조정
	ChangePageSetup1(3);

	pHwpCtrl.focus();
	//if (!bEditMode) { pHwpCtrl.focus(); }
	//ChangeCharShape(12);
}
function editToolBar() {
	if (typeof NetHwpMode != "undefined" && NetHwpMode == "SIMPLE") {
		pHwpCtrl.SetToolBar(-1, "TOOLBAR_MENU");
		pHwpCtrl.SetToolBar(1, "#1;1:기본도구모음, FileOpen, FileSaveAs, Print, " +
			"Separator, Undo, " +
			"CharShapeTypeFace, CharShapeHeight, FindDlg, " +
			"Separator, CharShapeBold, CharShapeItalic, CharShapeUnderline, " +
			"Separator, ParagraphShapeAlignJustify, ParagraphShapeAlignLeft, ParagraphShapeAlignRight, ParagraphShapeAlignCenter, " +
			"ParaShapeLineSpacing, ViewZoom, " +
			"PictureInsertDialog, Hyperlink, InputCodeTable, " +
			"Separator, TableCreate, TablePropertyDialog, CellBorderFill");
	}
	else {
		pHwpCtrl.SetToolBar(-1, "TOOLBAR_MENU");
//		pHwpCtrl.SetToolBar(-1, "TOOLBAR_STANDARD");
		pHwpCtrl.SetToolBar(-1, "TOOLBAR_FORMAT");
		pHwpCtrl.SetToolBar(-1, "TOOLBAR_TABLE");
	}
	pHwpCtrl.EditMode = 1;
}
function readToolBar() {
	//pHwpCtrl.SetToolBar(1, "#1;1:기본도구모음, ViewZoomRibon, FileSaveAs, Print, FilePreview");
	pHwpCtrl.SetToolBar(1, "#1;1:기본도구모음, FileSaveAs, Print, ViewZoom,FindDlg");
	pHwpCtrl.EditMode = 0;
}
function HwpEditMode(bMode) {
	if (bMode) {
		editToolBar();
		pHwpCtrl.ShowToolBar(true);
		pHwpCtrl.ShowStatusBar(false);
		pHwpCtrl.AutoShowHideToolBar(true);
	}
	else {
		readToolBar();
		pHwpCtrl.ShowToolBar(false);
		pHwpCtrl.ShowStatusBar(false);
		pHwpCtrl.AutoShowHideToolBar(true);
	}
	bEditMode = bMode;
}
function HwpUrlOpen(url) {
	pHwpCtrl.Open(url, "", "");
	pHwpCtrl.Run("MoveDocBegin");
}
function HwpSave(path) {
	pHwpCtrl.SaveAs(path);
	pHwpCtrl.Clear(3);
}
function InsertTextToField(field, text) {
	pHwpCtrl.MoveToField(field, true, true, false);
	var act = pHwpCtrl.CreateAction("InsertText");
	var set = act.CreateSet();
	set.SetItem("Text", text);
	act.Execute(set);
}
function InsertFileToField(field, file) {
	pHwpCtrl.MoveToField(field, true, true, false);
	pHwpCtrl.Insert(file, "", "")
}
function SetEditMode(m) {
	if (m) pHwpCtrl.EditMode = 1; // 일반 편집모드
	else pHwpCtrl.EditMode = 0;   // 읽기전용
}
function SetFieldView(opt) {
	return pHwpCtrl.SetFieldViewOption(opt);
}
function MoveStartCursor() {
	return pHwpCtrl.MovePos(2, 0, 0);
}
function MoveEndCursor() {
	return pHwpCtrl.MovePos(3, 0, 0);
}
function ChangeCharShape(FontSize)
{
	var act = pHwpCtrl.CreateAction("CharShape");
	var set = act.CreateSet();
	act.GetDefault(set);
	set.SetItem("Height", FontSize*100);     //글자크기 20
	act.Execute(set);
}
function OnStart() {
	var ff = document.all;
	alert("bEditMode : "+bEditMode+"    || bNewDoc : "+bNewDoc);

	if(bEditMode && bNewDoc){   // 새글
		if(ff.blbr_edmd_dscd.value == "2"){
			initNetHwp();
			initViewProp();
		}
	}else
	if(bEditMode && !bNewDoc){  // 수정
		if(ff.blbr_edmd_dscd.value == "2"){
			initNetHwp();
			initViewProp();
			if(bNewWindow)
				EditorModeChange("5");
			else
				EditorModeChange("6");
		}
	}else
	if(!bEditMode){     // 읽기
		alert("111");
		if(ff.blbr_edmd_dscd.value == "2"){
			alert("222");
			initNetHwp();
			initViewProp();
			EditorModeChange("7");
		}
	}
}
/********* 에디터 모드변경 ************/
function EditorChange(value){
	var ff = document.writeform;
	var msg = "\n☞ 에디터를 변경하시면\n\n현재 작성중인 본문내용이 수정전 상태로 되돌아가고,\n수정이나 답변이 아닌 새글로 등록됩니다.\n\n에디터 모드를 변경하시겠습니까?";
	if(confirm(msg)){
		//location.href='/ibkba/nethwp/write.jsp?code=2&page=1&blbr_edmd_dscd='+value;
		ff.blbr_edmd_dscd.selectedIndex = value;
		ff.target="bodyFrame";
		ff.action="/ibkba/nethwp/write.jsp";
		ff.submit();
	}else{
		if(ff.blbr_edmd_dscd.selectedIndex == 0)
			ff.blbr_edmd_dscd.selectedIndex = 1;
		else
			ff.blbr_edmd_dscd.selectedIndex = 0;
	}
}
/******** 에디터모드변경시 변환모드적용 *********/
function EditorModeChange(value){
	ff = document.all;
	if(ff.blbr_edmd_dscd.value == "2"){
		switch (value){
			case "5":   // 새창으로 첨부HWP파일 보기
				pHwpCtrl.open(sURL);//,"HWP");
				break;
			case "6":   // 읽기, 수정후 다른이름으로저장
			/*
				var temp = aURL + openFileNm.value;
				pHwpCtrl.open(temp);//,"HWP");
			*/
				SetTextFile();
				MoveStartCursor();
				break;
			case "7":   // 읽기
			//	"SetIDTable.hwp"
			/*
				var cURL = aURL + openFileNm.value;
				pHwpCtrl.open(cURL);//,"HWP");
			*/
			alert("333");
				//SetTextFile();
				MoveStartCursor();
				pHwpCtrl.EditMode = 0;
				break;
			case "8":   // 새문서 작성후 다른이름으로 저장
				break;
		}
	}
}

function HwpOpenPop(openFileNm, no, date, fileno){
	var cURL = "http://" + location.host + "/iFrame?Class=ibkib.bs.servlet.IBBS0601Svl&cmd=viewAtacheFile&openFileNm="+openFileNm + "&no=" + no + "&fileno=" + fileno + "&date=" + date;
	window.open(cURL,"SubForm","width=800,height=600,left=170,top=50,resizable=1");
}


function HwpOpen(code, num, seq, pagestatus, pagetitle, viewType){
	var cURL, vseq, vpagestatus = "", vpagetitle = "", vviewType = "";
	if(HwpOpen.arguments.length == 1){
		var openFileNm  = HwpOpen.arguments[0];
		HwpOpenPop(openFileNm);
		return;
	}else
	if(HwpOpen.arguments.length == 3){
		vseq        = HwpOpen.arguments[2];
	}else
	if(HwpOpen.arguments.length == 4){
		vseq        = HwpOpen.arguments[2];
		vpagestatus = HwpOpen.arguments[3];
	}else
	if(HwpOpen.arguments.length == 5){
		vseq        = HwpOpen.arguments[2];
		vpagestatus = HwpOpen.arguments[3];
		vpagetitle  = HwpOpen.arguments[4];
	}else
	if(HwpOpen.arguments.length == 6){
		vseq        = HwpOpen.arguments[2];
		vpagestatus = HwpOpen.arguments[3];
		vpagetitle  = HwpOpen.arguments[4];
		vviewType   = HwpOpen.arguments[5];
	}

	cURL = "http://" + location.host + "/iFrame?Class=ibkib.bs.servlet.IBBS0601Svl&cmd=viewAtacheFile&code="+ code +"&num="+ num +"&seq="+ vseq +"&pageStatus="+ vpagestatus +"&pageTitle="+ vpagetitle +"&viewType="+ vviewType + "&openFileNm="+document.all.openFileNm.value;

	window.open(cURL,"SubForm","width=800,height=600,left=170,top=50,resizable=1");
}

function GetCurrFileName(code, num) {
	return localPath + SYS_PREFIX + "_" + num + SYS_HWPEXT;
//	return localPath + SYS_PREFIX + "_" + code + "_" + num + SYS_HWPEXT;
}


function ChangePageSetup1(vApplyTo)
{
 try{
	var act = pHwpCtrl.CreateAction("PageSetup");
	var sset = act.CreateSet();
	act.GetDefault(sset);
	var pset = sset.CreateItemSet("PageDef","PageDef");

	// 적용 범위 : 문서 전체
	sset.SetItem('ApplyTo', vApplyTo);

	//1mm = 283.465 HWPUNITs
	pset.SetItem('TopMargin', 5401);
	pset.SetItem('BottomMargin', 5669);
	pset.SetItem('LeftMargin', 10251);
	pset.SetItem('RightMargin', 4251);
	pset.SetItem('HeaderLen', 0);
	pset.SetItem('FooterLen', 0);
	pset.SetItem('GutterLen', 0);
	pset.SetItem('PaperWidth', 72000);	//용지 가로 크기
	act.Execute(sset);

  }catch(err){
  alert('[ChangePageSetup] Error: name: ' + err.name + '; number: ' + err.number + '; message: ' + err.message);
 }
}